#include "lux.h"

lux::lux()
{
    //request memory for data
    data.msgs = (struct i2c_msg *)malloc(2 * sizeof(struct i2c_msg));
}

int lux::transfer(u_int8_t buf[], int size)
{
    u_int32_t la, lb, lc ,ld, sum;

    la = buf[size-1];
    lb = buf[size-2];
    lc = buf[size-3];
    ld = buf[size-4];

    la = 0xFFFFFF | (la << 24);
    lb = 0xFF00FFFF | (lb << 16);
    lc = 0xFFFF00FF | (lc << 8);
    ld = 0xFFFFFF00 | ld;

    sum = la & lb & lc & ld;

    return sum;
}


int lux::getExpo()
{
    fd = open(DEVICE_NAME, O_RDWR);
    if(fd < 0) {
      printf("Failed to open device %s.\n", DEVICE_NAME);
      return -1;
    }
    printf("open device %s Success.\n", DEVICE_NAME);
    //while (1) {
        /*-----0x00-----*/
          data.nmsgs = 2;
          data.msgs[0].buf = sendbuf;//申请内存为2个Byte
          data.msgs[0].len = 1;//目标reg的地址的长度
          data.msgs[0].addr = I2C_ADDR; //i2c设备地址
          data.msgs[0].flags = 0; //write flag
          data.msgs[0].buf[0] = REG_ADDR;//目标reg地址

          data.msgs[1].len = 1;//目标reg的地址的长度
          data.msgs[1].addr = I2C_ADDR; //i2c设备地址
          data.msgs[1].flags = 1; //read flag
          data.msgs[1].buf = recvbuf;//分配内存

          ioctl(fd,I2C_RDWR,(unsigned long)&data);
          luxBuff[0] = data.msgs[1].buf[0];
        /*-----0x01-----*/
          data.nmsgs = 2;
          data.msgs[0].buf = sendbuf;//申请内存为2个Byte
          data.msgs[0].len = 1;//目标reg的地址的长度
          data.msgs[0].addr = I2C_ADDR; //i2c设备地址
          data.msgs[0].flags = 0; //write flag
          data.msgs[0].buf[0] = 0x01;//目标reg地址

          data.msgs[1].len = 1;//目标reg的地址的长度
          data.msgs[1].addr = I2C_ADDR; //i2c设备地址
          data.msgs[1].flags = 1; //read flag
          data.msgs[1].buf = recvbuf;//分配内存

          ioctl(fd,I2C_RDWR,(unsigned long)&data);
          luxBuff[1] = data.msgs[1].buf[0];
          //printf("buf[1] = %x\n",data.msgs[1].buf[0]);
        /*-----0x02-----*/
          data.nmsgs = 2;
          data.msgs[0].buf = sendbuf;//申请内存为2个Byte
          data.msgs[0].len = 1;//目标reg的地址的长度
          data.msgs[0].addr = I2C_ADDR; //i2c设备地址
          data.msgs[0].flags = 0; //write flag
          data.msgs[0].buf[0] = 0x02;//目标reg地址

          data.msgs[1].len = 1;//目标reg的地址的长度
          data.msgs[1].addr = I2C_ADDR; //i2c设备地址
          data.msgs[1].flags = 1; //read flag
          data.msgs[1].buf = recvbuf;//分配内存

          ioctl(fd,I2C_RDWR,(unsigned long)&data);
          luxBuff[2] = data.msgs[1].buf[0];
          //printf("buf[2] = %x\n",data.msgs[1].buf[0]);
        /*-----0x03-----*/
          data.nmsgs = 2;
          data.msgs[0].buf = sendbuf;//申请内存为2个Byte
          data.msgs[0].len = 1;//目标reg的地址的长度
          data.msgs[0].addr = I2C_ADDR; //i2c设备地址
          data.msgs[0].flags = 0; //write flag
          data.msgs[0].buf[0] = 0x03;//目标reg地址

          data.msgs[1].len = 1;//目标reg的地址的长度
          data.msgs[1].addr = I2C_ADDR; //i2c设备地址
          data.msgs[1].flags = 1; //read flag
          data.msgs[1].buf = recvbuf;//分配内存

          ioctl(fd,I2C_RDWR,(unsigned long)&data);
          luxBuff[3] = data.msgs[1].buf[0];
          for(int i = 0; i < 4; i++)
          printf("%x\n",luxBuff[i]);

          light = transfer(luxBuff,4) / 1000;       //得到lux，除以55.6后得到umo
          expo = 5540400 * pow(light / 55.6, -1.075);
          printf("%d lux\n",light);
          printf("%d us\n",expo);
    //}

    close(fd);

    return expo;
}
